<template>
  <div class="login-container">
    <div class="flex-column login-left">
      <span>欢迎您！</span>
      <span>Welcome to the system</span>
    </div>
    <div class="login-form">
      <div class="flex-column form-header">
        <span>欢迎登录</span>
        <span>Welcome to login</span>
      </div>
      <el-form ref="form" :model="loginForm" :rules="rules">
        <el-form-item prop="account">
          <div class="flex-row login-item">
            <img src="~@/assets/login/dianhua@2x(1).png">
            <el-input v-model="loginForm['account']" autofocus placeholder="请输入用户名" @keyup.enter.native="handleLoginClick" />
          </div>
        </el-form-item>
        <el-form-item prop="password">
          <div class="flex-row login-item">
            <img src="~@/assets/login/mima@2x.png">
            <el-input
              ref="password"
              v-model="loginForm['password']"
              :type="passwordType"
              placeholder="请输入密码"
              clearable
              @keyup.enter.native="handleLoginClick"
            />
            <span
              v-if="passwordType === 'password'"
              class="show-password"
              @click="handlePassword"
            >
              <svg-icon icon-class="eye" />
            </span>
            <span
              v-else
              class="show-password"
              @click="handlePassword"
            >
              <svg-icon icon-class="eye-open" />
            </span>
          </div>
        </el-form-item>
        <el-form-item>
          <el-checkbox v-model="checked">记住密码</el-checkbox>
        </el-form-item>
      </el-form>
      <el-button type="primary" :loading="loading" class="login-button" @click="handleLoginClick">登录</el-button>
    </div>
  </div>
</template>

<script>

import { title } from '@/config'
export default {
  name: 'Login',
  data() {
    return {
      title,
      loginForm: {},
      rules: {
        account: [{ required: true, message: '用户名不能为空!' }],
        password: [{ required: true, message: '密码不能为空!' }]
      },
      checked: false,
      passwordType: 'password',
      loading: false,
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler(route) {
        this.redirect = (route.query && route.query.redirect) || '/'
      },
      immediate: true
    }
  },
  created() {
    document.body.style.overflow = 'hidden'
  },
  beforeUnmount() {
    document.body.style.overflow = 'auto'
  },
  mounted() {
  },
  methods: {
    handlePassword() {
      this.passwordType === 'password'
        ? (this.passwordType = '')
        : (this.passwordType = 'password')
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    // 登录
    handleLoginClick() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.loading = true
          this.$store
            .dispatch('user/login', this.loginForm)
            .then(() => {
              if (this.checked === true) {
                // 传入账号名，密码，和保存天数3个参数
                this.setCookie(this.loginForm.account, this.loginForm.password, 7)
              } else {
                // 清空Cookie
                this.clearCookie()
              }
              const routerPath =
                this.redirect === '/404' || this.redirect === '/401'
                  ? '/'
                  : this.redirect
              this.$router.push(routerPath).catch(() => {})
              this.loading = false
            })
            .catch(() => {
              this.loading = false
            })
        }
      })
    },
    // 设置cookie
    setCookie(c_name, c_pwd, exdays) {
      const exdate = new Date() // 获取时间
      exdate.setTime(exdate.getTime() + 24 * 60 * 60 * 1000 * exdays) // 保存的天数
      // 字符串拼接cookie
      window.document.cookie =
        `base_account= ${c_name};path=/;expires=` + exdate.toGMTString()
      window.document.cookie =
        'base_userPwd' + '=' + this.$baseEncrypt(c_pwd) + ';path=/;expires=' + exdate.toGMTString()
    },
    // 清除cookie
    clearCookie() {
      this.setCookie('', '', -1) // 修改2值都为空，天数为负1天就好了
    }
  }
}
</script>

<style lang="scss" scoped>
.login-container {
  position: relative;
  display: flex;
  align-items: center;
  width: 100vw;
  height: 100vh;
  .login-left {
    font-weight: 700;
    font-size: 20px;
    margin-left: 160px;
    color: #FFFFFF;
    span:nth-child(1) {
      font-size: 30px;
      letter-spacing: 3px;
    }
    span:nth-child(2) {
      font-size: 50px;
      line-height: 100px;
      letter-spacing: 13px;
    }
    span:nth-child(3) {
      font-size: 27px;
    }
  }
  .login-form {
    position: absolute;
    background: $base-content-background;
    border-radius: 8px;
    right: 160px;
    width: 380px;
    min-height: 400px;
    align-items: center;
    padding: 0 30px;
    box-sizing: border-box;
    .form-header {
      align-items: center;
      padding: 30px 0;
      color: #FFFFFF;
      font-weight: 600;
      span:nth-child(2) {
        color: #FFFFFF;
        margin-top: 5px;
      }
    }
    .login-item {
      align-items: center;
      height: 48px;
      line-height: 48px;
      border: 1px solid #F5F5F5;
      padding: 0 8px;
      box-sizing: border-box;
      border-radius: 5px;
      img {
        width: 15px;
        height: 15px;
        margin-right: 8px;
      }
    }
    .login-button {
      width: 100%;
      margin-top: 20px;
      height: 45px;
    }
  }
}
::v-deep {
  .el-input__inner {
    border: none;
  }
}
</style>
